package cn.vonce.sqlbean.example.controller;

import cn.vonce.sql.bean.Insert;
import cn.vonce.sql.uitls.IdBuilder;
import cn.vonce.sqlbean.example.common.Result;
import cn.vonce.sqlbean.example.enums.UserStatus;
import cn.vonce.sqlbean.example.model.entity.Essay;
import cn.vonce.sqlbean.example.model.entity.User;
import cn.vonce.sqlbean.example.service.EssayService;
import cn.vonce.sqlbean.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Insert 控制器
 *
 * @author Jovi
 * @version 1.0
 * @email imjovi@qq.com
 * @date 2024/2/27 15:18
 */
@RequestMapping
@RestController
public class InsertController {

    @Autowired
    private UserService userService;
    @Autowired
    private EssayService essayService;

    /**
     * 新增
     *
     * @return
     */
    @PostMapping("insert")
    public Result insert() {
        User user = new User();
        user.setAge(18);
        user.setUserName("Jovi");
        user.setNickName("Jovi Lam");
        user.setEmail("imjovi@qq.com");
        user.setGender(1);
        user.setMobilePhone("18888888888");
        user.setStatus(UserStatus.NORMAL);
        user.setPassword("123456");
        userService.insert(user);

        Essay essay = new Essay();
        essay.setUserId(user.getId());
        essay.setTitle("标题");
        essay.setContent("内容");
        if (essayService.insert(essay) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 新增
     *
     * @return
     */
    @PostMapping("insert2")
    public Result insert2() {
        Insert userInsert = new Insert<User>().column(
                        User::getId,
                        User::getAge,
                        User::getUserName,
                        User::getNickName,
                        User::getEmail,
                        User::getGender,
                        User::getMobilePhone,
                        User::getStatus,
                        User::getPassword)
                .values(IdBuilder.snowflake16(),
                        18,
                        "Jovi",
                        "Jovi Lam",
                        "imjovi@qq.com",
                        1,
                        "18888888888",
                        UserStatus.NORMAL,
                        "123456");
        if (userService.insert(userInsert) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 批量新增
     *
     * @return
     */
    @PostMapping("batchInsert")
    public Result batchInsert() {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setAge(i);
            user.setUserName("Jovi" + i);
            user.setNickName("Jovi Lam" + i);
            user.setEmail(i + "@qq.com");
            user.setGender(i % 2);
            user.setMobilePhone("1888888888" + i);
            user.setStatus(UserStatus.NORMAL);
            user.setPassword("123456");
            userList.add(user);
        }
        userService.insert(userList);

        List<Essay> essayList = new ArrayList<>();
        for (int i = 0; i < userList.size(); i++) {
            Essay essay = new Essay();
            essay.setUserId(userList.get(i).getId());
            essay.setTitle("标题" + i);
            essay.setContent("内容" + i);
            essayList.add(essay);
        }
        if (essayService.insert(essayList) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

}
